package com.douqu.game.configManager;

import com.douqu.game.MenuManager;
import com.douqu.game.core.factory.ConstantFactory;
import com.douqu.game.core.factory.LoadFactory;
import sun.net.util.IPAddressUtil;

import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author: miracle.
 * Description: configCreateUtils
 * Date: 2018/8/8 0008 下午 2:56
 * Huan Yu Copyright (c) 2017 All Rights Reserved.
 */
public class CCUtils {
    //数据源地址
    public static String DATA_SOURCE  = "data";
    public static String APPLICATION = "application.properties";
    public static String DEV_APPLICATION = "application-dev.properties";
    public static String PRODUCT_APPLICATION = "application-product.properties";
    public static String TEST_APPLICATION = "application-test.properties";
    public static String CONFIG = "config.properties";
    public static String LOG_4J = "log4j.properties";
    public static String PLATFORM = "platform.properties";

    /**
     * ip 正则验证
     * @param address
     * @return
     */
    public static boolean isBoolIp(String address){
        return IPAddressUtil.isIPv4LiteralAddress(address);
    }

    public static boolean isPort(String port){
        String regex = "^\\+?[1-9][0-9]*$";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(port);
        if(matcher.matches()){
            Integer p = Integer.valueOf(port);
            if (p > 65535)
                return false;
            return true;
        }
        return false;
    }

    public static boolean createConfig(String serverId,String activityId,String ip,String port,String userName,
                                       String password,String bucket,String log,String sql,String create,MenuManager manager){
        boolean flag = true;
        /**
         * 1.生成数据库
         * 2.分别拉取这些文件 替换其中的字段
         */
        manager.println("开始生成数据库");
        String result = ServerUtils.createNewServer(ip,port,userName,password,Integer.valueOf(serverId),sql);
        if (result.equals("OK")){
            manager.println("数据库创建成功");
        }else{
            manager.println(result);
            flag = false;
        }
        manager.println("开始生成" + APPLICATION);
        try{
            initApplication(create);
        }catch (IOException e){
            e.printStackTrace();
            manager.println("创建 "  + APPLICATION + "出错");
            flag = false;
        }
        manager.println("开始生成" + DEV_APPLICATION);
        String driver = getSql(ip,port,serverId);
        try {
            initDevApplication(create,serverId,driver,userName,password);
        }catch (IOException e){
            e.printStackTrace();
            manager.println("创建 "  + DEV_APPLICATION + "出错");
            flag = false;
        }
        manager.println("开始生成" + PRODUCT_APPLICATION);
        try {
            initProductApplication(create, serverId, driver, userName, password);
        }catch (IOException e){
            e.printStackTrace();
            manager.println("创建 "  + PRODUCT_APPLICATION + "出错");
            flag = false;
        }
        manager.println("开始生成" + TEST_APPLICATION);
        try {
            initTestApplication(create, serverId, driver, userName, password);
        }catch (IOException e){
            e.printStackTrace();
            manager.println("创建 "  + TEST_APPLICATION + "出错");
            flag = false;
        }
        manager.println("开始生成" + CONFIG);
        try {
            initConfig(create, serverId, log, activityId);
        }catch (IOException e){
            e.printStackTrace();
            manager.println("创建 "  + CONFIG + "出错");
            flag = false;
        }
        manager.println("开始生成" + LOG_4J);
        try {
            initLog(create);
        }catch (IOException e){
            e.printStackTrace();
            manager.println("创建 "  + LOG_4J + "出错");
            flag = false;
        }
        manager.println("开始生成" + PLATFORM);
        try {
            initPlatform(create, bucket);
        }catch (IOException e){
            e.printStackTrace();
            manager.println("创建 "  + PLATFORM + "出错");
            flag = false;
        }
        return flag;
    }

    public static String getSql(String ip,String port,String serverId){
        return "jdbc:mysql://" + ip + ":" + port + "/slg" + serverId + "?useUnicode=true&characterEncoding=utf-8&autoReconnect=true";
    }

    private static boolean initApplication(String create) throws IOException {
        InputStream inputStream = LoadFactory.getInputStream(DATA_SOURCE + File.separator + APPLICATION);
        String str = inputToString(inputStream);
        String name = create + File.separator + APPLICATION;
        writeMethod(name,str);
        return true;
    }

    private static boolean initDevApplication(String create,String serverId,String sql,String user,String password) throws IOException{
        InputStream inputStream = LoadFactory.getInputStream(DATA_SOURCE + File.separator + DEV_APPLICATION);
        String str = inputToString(inputStream);

        str = str.replace("{serverId1}",serverId + "5");
        str = str.replace("{dburl}", sql);
        str = str.replace("{username}",user);
        str = str.replace("{password}",password);
        str = str.replace("{dburl}",sql);
        str = str.replace("{username}",user);
        str = str.replace("{password}",password);

        String name = create + File.separator + DEV_APPLICATION;
        writeMethod(name,str);
        return true;
    }

    private static boolean initProductApplication(String create,String serverId,String sql,String user,String password) throws IOException{
        InputStream inputStream = LoadFactory.getInputStream(DATA_SOURCE + File.separator + PRODUCT_APPLICATION);
        String str = inputToString(inputStream);

        str = str.replace("{serverId1}",serverId + 5);
        str = str.replace("{dburl}", sql);
        str = str.replace("{username}",user);
        str = str.replace("{password}",password);
        str = str.replace("{dburl}",sql);
        str = str.replace("{username}",user);
        str = str.replace("{password}",password);

        String name = create + File.separator + PRODUCT_APPLICATION;
        writeMethod(name,str);
        return true;
    }

    private static boolean initTestApplication(String create,String serverId,String sql,String user,String password) throws IOException{
        InputStream inputStream = LoadFactory.getInputStream(DATA_SOURCE + File.separator + TEST_APPLICATION);
        String str = inputToString(inputStream);

        str = str.replace("{serverId1}",serverId + 5);
        str = str.replace("{dburl}", sql);
        str = str.replace("{username}",user);
        str = str.replace("{password}",password);
        str = str.replace("{dburl}",sql);
        str = str.replace("{username}",user);
        str = str.replace("{password}",password);

        String name = create + File.separator + TEST_APPLICATION;
        writeMethod(name,str);
        return true;
    }

    private static boolean initConfig(String create,String serverId,String log,String activityId) throws IOException{
        InputStream inputStream = LoadFactory.getInputStream(DATA_SOURCE + File.separator + CONFIG);
        String str = inputToString(inputStream);

        str = str.replace("{serverId1}",serverId + 1);
        str = str.replace("{serverId2}",serverId + 6);
        str = str.replace("{serverId3}",serverId + 7);
        str = str.replace("{serverId4}",serverId + 4);
        str = str.replace("{serverId5}",serverId + 3);
        str = str.replace("{log}",log + "/" + serverId + "/");
        str = str.replace("{activityId}",activityId);

        String name = create + File.separator + CONFIG;
        writeMethod(name,str);
        return true;
    }

    private static boolean initLog(String create) throws IOException {
        InputStream inputStream = LoadFactory.getInputStream(DATA_SOURCE + File.separator + LOG_4J);
        String str = inputToString(inputStream);
        String name = create + File.separator + LOG_4J;
        writeMethod(name,str);
        return true;
    }

    private static boolean initPlatform(String create,String bucket) throws IOException {
        InputStream inputStream = LoadFactory.getInputStream(DATA_SOURCE + File.separator + PLATFORM);
        String str = inputToString(inputStream);
        str = str.replace("{bucket}",bucket);
        String name = create + File.separator + PLATFORM;
        writeMethod(name,str);
        return true;
    }

    public static String inputToString(InputStream inputStream) throws IOException{
        StringBuffer sb = new StringBuffer();
        byte[] b = new byte[4096];
        for (int n;(n = inputStream.read(b)) != -1;){
            sb.append(new String(b,0,n));
        }
        return sb.toString();
    }

    public static void writeMethod(String name,String content) throws IOException {
        BufferedWriter out = new BufferedWriter(new FileWriter(name));
        out.write(content);
        out.close();
    }
}
